package com.qumitech.visualnovel.service.chat.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.chat.ChatEarnBlockCfgAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.chat.ChatEarnBlockAdminCfgDTO;
import com.qumitech.visualnovel.service.chat.ChatEarnService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ChatEarnServiceImpl implements ChatEarnService {

    private static final String saveChatEarnBlockCfgUrl = "/chat/earn/console/block/cfg";
    private static final String listChatEarnBlockCfgUrl = "/chat/earn/console/block/cfg";
    private static final String rmChatEarnBlockCfgUrl = "/chat/earn/console/block/cfg/";

    @Override
    public void saveChatEarnBlockCfg(ChatEarnBlockAdminCfgDTO cfgDTO) {
        String url = UrlConstant.SERVER_URL + saveChatEarnBlockCfgUrl;
        String rspBody = HttpInvokeUtil.httpPostJson(url, JSON.toJSONString(cfgDTO), UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public BaseRsp listChatEarnBlockCfg(ChatEarnBlockCfgAdminQueryDTO queryAdminDTO) {
        String url = UrlConstant.SERVER_URL + listChatEarnBlockCfgUrl;
        Map<String, String> paramMap = new HashMap<>();
        if(StringUtils.isNotBlank(queryAdminDTO.getPackageName())){
            paramMap.put("package_name", queryAdminDTO.getPackageName());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getAppChannel())){
            paramMap.put("app_channel", queryAdminDTO.getAppChannel());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getAppVersion())){
            paramMap.put("app_version", queryAdminDTO.getAppVersion());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getStartTime())){
            paramMap.put("startTime", queryAdminDTO.getStartTime());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getEndTime())){
            paramMap.put("endTime", queryAdminDTO.getEndTime());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getEndTime())){
            paramMap.put("offset", queryAdminDTO.getEndTime());
        }
        if(StringUtils.isNotBlank(queryAdminDTO.getEndTime())){
            paramMap.put("limit", queryAdminDTO.getEndTime());
        }
        String rspBody = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp;
    }

    @Override
    public void rmChatEarnBlockCfg(String configId) {
        String url = UrlConstant.SERVER_URL + rmChatEarnBlockCfgUrl + configId;
        String rspBody = HttpInvokeUtil.httpDelete(url, null);
        BaseRsp rsp = JSONObject.parseObject(rspBody, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }
}
